带参数的方法的Powermockito doNothing 您所在的位置:网站首页 powermock donothing 带参数的方法的Powermockito doNothing

带参数的方法的Powermockito doNothing

#带参数的方法的Powermockito doNothing| 来源: 网络整理| 查看: 265

百度翻译此文   有道翻译此文 问题描述

I've developed an application in Java and I'm trying to create unit tests using Powermockito (I should add that I'm new to unit testing).

I have a class called Resource which has a static method called readResources:

public static void readResources(ResourcesElement resourcesElement);

ResourcesElement is also coded by me. In testing, I want to create my own Resource, so I want the above method to do nothing. I tried using this code:

PowerMockito.spy(Resource.class); PowerMockito.doNothing().when(Resource.class, "readResources", Matchers.any(ResourcesElement.class));

The unit test throws an exception:

org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detected here: -> at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36)

Powermockito also suggest that I should use thenReturn or thenThrow after when, but it seems that the method 'when' returns void when it is called after doNothing (which is logical). If I try:

PowerMockito.when(Resource.class, "readResources", Matchers.any(ResourcesElement.class)).....

doNothing is not an option after when.

I managed to make methods without arguments to do nothing, using the 2 arguments version of the method. For example:

PowerMockito.doNothing().when(Moduler.class, "startProcessing");

This works (startProcessing doesn't take any arguments).

But how can I make methods that do take arguments to do nothing with Powermockito?

推荐答案

You can find a fully functional example below. Since you didn't post the complete example, I can only assume that you did not annotate the test class with @RunWith or @PrepareForTest because the rest seems fine.

@RunWith(PowerMockRunner.class) @PrepareForTest({Resource.class}) public class MockingTest{ @Test public void shouldMockVoidStaticMethod() throws Exception { PowerMockito.spy(Resource.class); PowerMockito.doNothing().when(Resource.class, "readResources", Mockito.any(String.class)); //no exception heeeeere! Resource.readResources("whatever"); PowerMockito.verifyStatic(); Resource.readResources("whatever"); } } class Resource { public static void readResources(String someArgument) { throw new UnsupportedOperationException("meh!"); } } 其他推荐答案

Why go through so much trouble just so that your method does not do anything. Just calling PowerMockito.mockStatic(Resource.class) should replace all static methods in your class with default stubs which basically mean they do nothing.

Unless you do want to change the behavior of your method to actually do something just calling PowerMockito.mockStatic(Resource.class) should suffice. Ofcourse this also means all static methods in the class are stubbed which you need to consider.

其他推荐答案

If doNothing() isn't working you can hack it a bit using the PowerMockito.doAnswer(). This lets you mock into void methods that are supposed to do something, like setting values, etc. If doNothing() doesn't work, using a blank doAnswer() should work fine.

Example:

PowerMockito.doAnswer(new org.mockito.stubbing.Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return null; //does nothing } }).when(mockObject).methodYouWantToDoNothing(args);


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有